package com.liy.cloud.base.rest;


import com.liy.cloud.common.base.entity.BaseGoodsPropertyEntity;
import com.liy.cloud.common.base.service.BaseGoodsPropertyService;
import com.das.framework.common.annotation.MethodPermission;
import com.das.framework.common.constants.PermissionCodes;
import com.das.framework.common.extvo.ResultVO;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.das.framework.common.annotation.ActionPermission;
import com.das.framework.common.web.controller.BaseCrudController;

import javax.validation.Valid;
import java.util.List;

/**
* @Author:  zhangw
* @Date:    2018/6/20  16:08
* @Description: 物品属性管理
*
**/
@RestController
@RequestMapping("/goods/property")
@ActionPermission(code="BASE.GOODSPROPERTY", description="物品属性管理")
public class BaseGoodsPropertyRest extends BaseCrudController<BaseGoodsPropertyService, BaseGoodsPropertyEntity> {
	
	protected static Logger log = LoggerFactory.getLogger(BaseGoodsPropertyRest.class);
	
	@Autowired
	private BaseGoodsPropertyService baseGoodsPropertyService;
	

    /**
     * @Description: 批量删除
     **/
    @PostMapping("/delete/batch")
    @MethodPermission(codes = {"DELETE"}, description = "删除")
    public ResultVO deleteBatch(@RequestBody() List<String> ids) {
        try {
            baseGoodsPropertyService.deleteBatch(ids);
            return new ResultVO(true, "删除成功:", "200");
        } catch (Exception var4) {
            log.error(var4.getMessage(), var4);
            return new ResultVO(false, "删除时发生错误:" + var4.getMessage(), "500");
        }
    }

    @PostMapping("/query/list")
    @MethodPermission(codes = PermissionCodes.SEARCH, description = "列表查询")
    public ResultVO saveRolePermission(@RequestBody() List<String> ids) {
        ResultVO resultVO = new ResultVO();
        try {
            resultVO.setData(baseGoodsPropertyService.getAccountArrByIds(ids));
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            resultVO.setSuccess(false, "500");
            resultVO.setMessage("查询时发生错误:" + e.getMessage());
        }
        return resultVO;
    }


    @RequestMapping(value = {"/add/goods/property"}, method = {RequestMethod.POST})
    @MethodPermission(codes = {"INSERT"}, description = "新增")

    public ResultVO addGoodsProperty(@RequestBody @Valid BaseGoodsPropertyEntity baseGoodsPropertyEntity) {

        try {
            //新增前先校验编号是否重复
            BaseGoodsPropertyEntity goodsProperty = this.baseService.findById(baseGoodsPropertyEntity.getId());
            if(goodsProperty != null) {
                return new ResultVO(false, "编号存在重复，请重新输入", "500");
            }

            baseGoodsPropertyService.addGoodsProperty(baseGoodsPropertyEntity);
            return new ResultVO(true, "新增成功", "200", baseGoodsPropertyEntity);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return new ResultVO(false, "新增时发生错误:" + e.getMessage(), "500");
        }
    }

}
